package cn.byapps.hw.service.impl;

import cn.byapps.hw.dao.ClazzDao;
import cn.byapps.hw.dao.StudentDao;
import cn.byapps.hw.entity.Clazz;
import cn.byapps.hw.entity.Student;
import cn.byapps.hw.service.StudentService;
import cn.byapps.hw.utils.MD5Utils;
import cn.byapps.hw.vo.StudentVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.*;

@Transactional
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentDao studentDao;
    @Resource
    private ClazzDao clazzDao;

    @Override
    public void save(Student item) {
        Student student = new Student();
        student.setId(item.getId());
        student.setName(item.getName());
        student.setPassword(MD5Utils.md5(item.getPassword()));
        student.setPhone(item.getPhone());
        studentDao.save(student);
    }

    @Override
    public int total() {
        return (int) studentDao.count();
    }

    @Override
    public int total(Integer clazzId) {
        Clazz clazz = clazzDao.getOne(clazzId);
        return (int) studentDao.countByClazz(clazz);
    }

    @Override
    public int totalBySearch(String search) {
        return (int) studentDao.countByClazz(search);
    }

    @Override
    public void deleteById(String id) {
        studentDao.deleteById(id);
    }

    @Override
    public void update(Student student) {
        Student s = studentDao.getOne(student.getId());
        if (s != null) {
            s.setName(student.getName());
            s.setPassword(MD5Utils.md5(s.getPassword()));
            s.setPhone(student.getPhone());
            s.setClazzs(s.getClazzs());
        }
    }

    @Override
    public void addByClazz(Student student, Integer clazzId) {
        Optional<Student> s = studentDao.findById(student.getId());
        Student student1 = s.isPresent() ? s.get() : null;
        if (student1 == null) {
            Set<Clazz> clazzes = new HashSet<>();
            clazzes.add(clazzDao.getOne(clazzId));
            studentDao.save(new Student(student.getId(), student.getName(), MD5Utils.md5(student.getPassword()), student.getPhone(), clazzes));
        } else {
            Set<Clazz> clazzes = student1.getClazzs();
            clazzes.add(clazzDao.getOne(clazzId));
            student1.setClazzs(clazzes);
        }
    }

    @Override
    public void batchAdd(List<Student> students, Integer clazzId) {
        for (Student student : students) {
            Optional<Student> s = studentDao.findById(student.getId());
            Student student1 = s.isPresent() ? s.get() : null;
            if (student1 == null) {
                Set<Clazz> clazzes = new HashSet<>();
                clazzes.add(clazzDao.getOne(clazzId));
                studentDao.save(new Student(student.getId(), student.getName(), MD5Utils.md5(student.getPassword()), student.getPhone(), clazzes));
            } else {
                Set<Clazz> clazzes = student1.getClazzs();
                clazzes.add(clazzDao.getOne(clazzId));
                student1.setClazzs(clazzes);
            }
        }
    }


    @Override
    public List<StudentVO> findVO(Integer pos, Integer rows) {
        int pageNumber = (pos + rows - 1) / rows;
        Pageable pageable = PageRequest.of(pageNumber, rows);
        Page<Student> page = studentDao.findAll(pageable);
        List<Student> students = page.getContent();
        List<StudentVO> studentVOS = new ArrayList<>();
        for (Student student : students) {
            studentVOS.add(new StudentVO(student.getId(), student.getName(), student.getPassword(), student.getPhone(), student.getClazzs()));
        }
        return studentVOS;
    }

    @Override
    public List<StudentVO> findVO(Integer pos, Integer rows, String search) {
        int pageNumber = (pos + rows - 1) / rows;
        Pageable pageable = PageRequest.of(pageNumber, rows);
        List<Student> students = studentDao.findPageByClazz(pageable, search);
        List<StudentVO> studentVOS = new ArrayList<>();
        for (Student student : students) {
            studentVOS.add(new StudentVO(student.getId(), student.getName(), student.getPassword(), student.getPhone(), student.getClazzs()));
        }
        return studentVOS;
    }

    @Override
    public List<StudentVO> findVO(Integer pos, Integer rows, Integer clazzId) {
        int pageNumber = (pos + rows - 1) / rows;
        Pageable pageable = PageRequest.of(pageNumber, rows);
        List<Student> students = studentDao.findPageByClazz(pageable, new Clazz(clazzId, null, null));
        List<StudentVO> studentVOS = new ArrayList<>();
        for (Student student : students) {
            studentVOS.add(new StudentVO(student.getId(), student.getName(), student.getPassword(), student.getPhone(), student.getClazzs()));
        }
        return studentVOS;
    }


}
